var EpgAjaxGrid = Class.create(EpgGrid,{
	initialize: function($super, id, opts,links) {
        $super(id,opts,links);
        if(opts.pageInfo) {
        	this.ajax(opts.pageInfo.pageNo);
        }
    },
    refershScollerBar : function() {
    	if( !document.getElementById( this.id + "Scroller_img")){
    		return 
    	}
    	var barBg = $("#" + this.id + "Scroller");
		var bar = $("#" + this.id + "Scroller_img");
		var scroller = $("#" + this.id + "Scroller_bar");
		var cssHeight = barBg.css("height");
		cssHeight = cssHeight.replace("px", "");
		var height = parseInt(cssHeight);
		var barHeight = 0;
		var pageCount = this.opts.pageInfo.pageCount;
		if(this.opts.refresh == 1){
			pageCount = parseInt(pageCount)+1;
		}
		if (this.opts.pageInfo.pageCount > 0) {
			barHeight = height / pageCount;
			bar.css("height", barHeight + "px");
		}
		var pageNo = this.opts.pageInfo.pageNo;
		if(this.opts.refresh == 1){
			pageNo = parseInt(pageNo)+1;
		}
		barTop = barHeight * (pageNo - 1);
		if (barTop + barHeight > height) {
			barTop = height - barHeight;
		}
		scroller.css("height",barHeight + "px");
		scroller.css("top", barTop + "px");
	},
	setPageInfo : function(pageInfo) {
		this.opts.pageInfo = pageInfo;
		this.refershScollerBar();
		if(this.opts.pageInfoDiv){
			if(this.opts.refresh == 1){
				$("#"+this.opts.pageInfoDiv).html((parseInt(this.opts.pageInfo.pageNo)+1) + "/" + (parseInt(this.opts.pageInfo.pageCount)+1));
			}else{
				$("#"+this.opts.pageInfoDiv).html(this.opts.pageInfo.pageNo + "/" + this.opts.pageInfo.pageCount);
			}
		}
	},
	clearLayout:function(lastPageSize){
		if(this.opts.layoutName){
			for(var i=lastPageSize-1;i<this.size;i++){
				$("#"+this.opts.layoutName+i).hide();
			}
		}
	},
	
	ajax:function(pageNo){
		var url = EPG.appendParam(this.opts.url,this.opts.pageName+"_idx",pageNo);
		var opts = this.opts;
		$.ajax({
			method:'get',
			url:url,
			context:this,
			async:false,
			dataType:opts.dataType,
			success:function(data){
			if(data.pageInfo.pageNo == data.pageInfo.pageCount){
				this.clearLayout(data.resultSet.length);
			}
			  //for循环中需要pageInfo
			 if(data.pageInfo){
				  this.setPageInfo(data.pageInfo);	
			  }
			 
			 if(data.pageBean){
				  this.setPageInfo(data.pageBean);	
			  }
			 
			  if(this.opts.dataType=="json"){
				 var resultSet =  data.resultSet;
				 if(this.opts.pageInfo){
			 	  this.opts.pageInfo.pageNo = pageNo;
			     } 
				 for(var i=0;i<resultSet.length;i++){
					var item = resultSet[i];
					var link =  epgPage.findLink(this.links[i]);
					link.disable = false;
					link.href = item.url;
					if(this.opts.render){
						this.opts.render(this,i,item,link);
					}
					$("#"+this.links[i]+"_layout").css("display","block");
					
				 }
				 for(var i=resultSet.length;i<this.links.length;i++){
					 $("#"+this.links[i]+"_layout").css("display","none");
					 var link =  epgPage.findLink(this.links[i]);
					 link.href = "#";
					 link.disable = true;
				 }
				 this.setLinks(resultSet.length);
			  }
			  //this.setPageInfo(data.pageInfo);
			  /*if(this.links.length>0){
				  epgPage.focus(this.links[0]);
			  }*/
			  
			}
		});
	},
	reload:function(url){
		this.opts.url = url;
		this.ajax(1);
	},
	pageUp : function(idx) {
		if (this.opts.pageInfo.pageNo > 1) {
			this.opts.pageInfo.pageNo--;
			this.ajax(this.opts.pageInfo.pageNo);
			var index = idx + (this.opts.row-1)*this.opts.col;
			if(this.links.length> index){
			  epgPage.switchFocus(this.links[index]);
		    }
			return true;
		}else{
			if(this.opts.refresh == 1){
				var currentUrl = window.location.href;
				currentUrl = addUrlParameter(currentUrl,"p_idx",1);
				currentUrl = addUrlParameter(currentUrl,"efid",this.links[idx]);
				window.location.href = currentUrl;
				return true;
			}
		}
		return false;
	},
	pageDown : function(idx) {
		var idx = idx%this.opts.col;
		var lastPageSize = this.opts.pageInfo.recordCount%this.size;
		if(lastPageSize==0 && this.opts.pageInfo.recordCount>0){
			lastPageSize = this.size;
		}
		if (this.opts.pageInfo.pageNo < this.opts.pageInfo.pageCount) {
			this.opts.pageInfo.pageNo++;
			this.ajax(this.opts.pageInfo.pageNo);
			
			/*if(this.links.length>0){
				  epgPage.switchFocus(this.links[0]);
			}*/
			if(idx < lastPageSize){
				epgPage.switchFocus(this.links[idx]);
			}else{
				epgPage.switchFocus(this.links[lastPageSize-1]);
			}
			return true;
		}
		return false;
	}
});
